package cn.kilo.domain.support;

/**
 * @author kilok
 */
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

public class BufferedImageUtils {

    public static MultipartFile convertToMultipartFile(BufferedImage image, String originalFilename) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(image, "jpeg", os);
        return new BufferedImageMultipartFile(os.toByteArray(), originalFilename);
    }

    public static void main(String[] args) throws IOException {
        // 读取图片文件
        BufferedImage image = ImageIO.read(new File("path/to/image.jpg"));
        // 转换为MultipartFile对象
        MultipartFile multipartFile = convertToMultipartFile(image, "image.jpg");
        // 使用MultipartFile对象进行其他操作
        // ...
    }
}